package br.com.management.mentor.security.auth.services;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import br.com.management.mentor.model.entities.auth.User;
import br.com.management.mentor.model.repositories.UsersRepository;

public class JPAUserDetailServiceImpl implements UserDetailsService {

	@Autowired
	private UsersRepository repository;

	public UserDetails loadUserByUsername( String username ) throws UsernameNotFoundException {

		if ( StringUtils.isEmpty( username ) ) throw new UsernameNotFoundException( username );

		User user = repository.getByUsername( username );
		if ( user == null ) throw new UsernameNotFoundException( username );

		return user;
	}

}